package com.youbug.common.interceptor;

import com.baomidou.mybatisplus.extension.plugins.inner.InnerInterceptor;
import com.youbug.common.constant.SystemConstant;
import com.youbug.system.entity.User;
import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.mapping.MappedStatement;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.sql.SQLException;
@Component
public class MyBatisTestInterceptor implements InnerInterceptor {

    @Autowired
    private HttpServletRequest request;
    private String[] unInterceptorTable = {"ChatMapper","MessageMapper","CustomerMapper","LoginLogMapper","GamePlayer"};

    @Override
    public boolean willDoUpdate(Executor executor, MappedStatement ms, Object parameter) throws SQLException {
        String id = ms.getId();
        for (String s : unInterceptorTable) {
            if (id.contains(s)) {
                return true;
            }
        }
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute(SystemConstant.CURRENT_USER);
        if(user == null){
            return true;
        }
        String username = user.getUsername();
        if(SystemConstant.TEST_USER.equals(username)){
            return false;
        }
        return true;
    }
}
